import request from '../http/request'
import type { ApiResponse, User } from '../http/types'

export interface LoginParams {
  username: string
  password: string
}

export interface RegisterParams {
  username: string
  password: string
  email?: string
}

export const authApi = {
  // 用户登录
  login: (data: LoginParams): Promise<ApiResponse<{ token: string; user: User }>> => {
    return request.post('/auth/login', data)
  },

  // 用户注册
  register: (data: RegisterParams): Promise<ApiResponse<User>> => {
    return request.post('/auth/register', data)
  },

  // 获取当前用户信息
  getCurrentUser: (): Promise<ApiResponse<User>> => {
    return request.get('/auth/me')
  },

  // 刷新token
  refreshToken: (): Promise<ApiResponse<{ token: string }>> => {
    return request.post('/auth/refresh')
  },

  // 检查token有效性
  validateToken: (token: string): Promise<ApiResponse<boolean>> => {
    return request.post('/auth/validate-token', { token: token })
  },

  // 用户登出
  logout: (): Promise<ApiResponse> => {
    return request.post('/auth/logout')
  },
}
